<template>
  <div class="indexContain">
    <div class="login-bg">
      <img src="~@/assets/images/login.jpg" >
    </div>
    <div>
       <el-form
      v-loading="loading"
      element-loading-text="正在登录"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :rules="rules"
      ref="loginForm"
      :model="loginForm"
      class="loginContainer"
    >
      <h3 class="loginTitle">文化社区和视频平台网站</h3>
      <el-form-item label="邮箱：" prop="email">
        <el-input
          type="text"
          v-model="loginForm.email"
          auto-complete="false"
          placeholder="请输入您的邮箱账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码：">
        <el-input
          auto-complete="false"
          type="password"
          placeholder="请输入您的密码"
          v-model="loginForm.password"
        ></el-input>
      </el-form-item>
      <el-button
        type="primary"
        style="width: 100%; margin: 10px 0 20px 0"
        @click="submitLogin"
        >登录</el-button
      >
      </el-form>
    </div>
  </div>
</template>

<script>
import cookie from "js-cookie";
import md5 from "md5-js";
import axios from "axios";
export default {
  name: "index",
  data() {
    return {
      loginForm: {
        email: "1330074712@qq.com",
        password: "123",
      },
      loading: false,
      rules: {
        email: [
          { required: true, message: "请输入您的邮箱账号", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入您的密码", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    if (cookie.get("token")) {
      this.$router.push({path:'/hello'})
    }
  },
  methods: {
    //表单内容校验
    submitLogin() {
      let data = new FormData();
      data.append("account", this.loginForm.email);
      data.append("password", md5(this.loginForm.password));
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          axios({
            method: "post",
            url: "http://114.132.229.158:10001/login/admin",
            data,
          }).then((response) => {
            if (response.data.code != 500) {
              this.loading = false;
              const tokenStr = response.data.data.token;
              if (tokenStr != "") {
                cookie.set("token", tokenStr, { expires: 7, path: "/" });
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
              } else {
                this.$message.error("账号或密码错误");
              }
              // this.$router.replace("/hello");
              this.$router.replace("/home");
            } else {
              this.$message.error("账号或密码错误");
              return false;
            }
          });
        } else {
          this.$message.error("请输入所有字段！");
          return false;
        }
      });
    },
  },
};
</script>

<style>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 160px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cacaca;
}
.loginTitle {
  margin: 0 auto 30px auto;
  text-align: center;
}
.login-bg {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  z-index: -1;
}

.login-bg > img {
  width: 100%;
  height: 100%;
  border: 0px;
}
</style>
